<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_PhotoController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {
		try{
			$photo_id = $this->_getParam('id');
			if($photo_id != ""){
				$photo_mapper = new Api_Model_PhotoMapper();
				$photo_mapper->deletePhoto($photo_id);

				throw new Exception(json_encode(array('meta' => 'OK')),200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getAction() {

		try{
			$photo_id = $this->_getParam('param2');
			$user_id = $this->_getParam('param1');
			if($photo_id != "" || $user_id != ''){
				$photo_mapper = new Api_Model_PhotoMapper();
				$photo = $photo_mapper->getPhotoById($photo_id, $user_id);

				$this->getresponse()->appendBody(json_encode($photo));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}

	}

	public function headAction() {

	}

	public function indexAction() {
		try{
			$photo_mapper = new Api_Model_PhotoMapper();
			$resource = $this->_getParam('resource');
			$photo_limit = $this->_getParam('batch');
			if ($resource == 'location'){

				$location_id = $this->_getParam('id');

				if ($location_id != NULL){

					$photo = $photo_mapper->getAllLocationPhoto($location_id, $photo_limit);
					$this->getresponse()->appendBody(json_encode($photo));
					$this->getResponse()->setHttpResponseCode(200);
				} else {
					throw new Exception(json_encode(array('meta' => 'Bad request')),400);
				}
			} elseif ($resource == 'user'){
				$user_id = $this->_getParam('id');

				if ($user_id != NULL){

					$photo = $photo_mapper->getAllUserPhoto($user_id, $photo_limit);
					$this->getresponse()->appendBody(json_encode($photo));
					$this->getResponse()->setHttpResponseCode(200);
				} else {
					throw new Exception(json_encode(array('meta' => 'Bad request')),400);
				}
			} elseif ($resource == 'tour'){
				$tour_id = $this->_getParam('id');

				if ($tour_id != NULL){

					$photo = $photo_mapper->getAllTourPhoto($tour_id, $photo_limit);
					$this->getresponse()->appendBody(json_encode($photo));
					$this->getResponse()->setHttpResponseCode(200);
				} else {
					throw new Exception(json_encode(array('meta' => 'Bad request')),400);
				}
			} elseif ($resource == "") {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			 
			$photo_mapper = new Api_Model_PhotoMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['user_id'] != ""){
				$link = $photo_mapper->photoUpload($data);
				$this->getresponse()->appendBody(json_encode($link));
				$this->getResponse()->setHttpResponseCode(201);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {

	}
}


